package com.shijiu.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
 * @Author xlj
 * @Date Created in  2023/2/23 17:25
 * @DESCRIPTION:  全局统一返回结果
 * @Version V1.0
 */
@Data
@ApiModel(value = "全局统一返回结果")
@SuppressWarnings("all")
public class ResultTest<T> {

   @ApiModelProperty(value = "返回码")
   private Integer code;

   @ApiModelProperty(value = "返回消息")
   private String message;

   @ApiModelProperty(value = "返回数据")
   private T data;
   private Long total;

   public ResultTest(){}

   protected static <T> ResultTest<T> build(T data) {
      ResultTest<T> result = new ResultTest<T>();
      if (data != null)
         result.setData(data);
      return result;
   }

   public static <T> ResultTest<T> build(T body, ResultCodeEnum resultCodeEnum) {
      ResultTest<T> result = build(body);
      result.setCode(resultCodeEnum.getCode());
      result.setMessage(resultCodeEnum.getMessage());
      return result;
   }

   public static <T> ResultTest<T> build(Integer code, String message) {
      ResultTest<T> result = build(null);
      result.setCode(code);
      result.setMessage(message);
      return result;
   }

   public static<T> ResultTest<T> ok(){
      return ResultTest.ok(null);
   }

   /**
    * 操作成功
    * @param data
    * @param <T>
    * @return
    */
   public static<T> ResultTest<T> ok(T data){
      ResultTest<T> result = build(data);
      return build(data, ResultCodeEnum.SUCCESS);
   }

   public static<T> ResultTest<T> fail(){
      return ResultTest.fail(null);
   }

   /**
    * 操作失败
    * @param data
    * @param <T>
    * @return
    */
   public static<T> ResultTest<T> fail(T data){
      ResultTest<T> result = build(data);
      return build(data, ResultCodeEnum.FAIL);
   }

   public ResultTest<T> message(String msg){
      this.setMessage(msg);
      return this;
   }

   public ResultTest<T> code(Integer code){
      this.setCode(code);
      return this;
   }

   public boolean isOk() {
      if(this.getCode().intValue() == ResultCodeEnum.SUCCESS.getCode().intValue()) {
         return true;
      }
      return false;
   }
}
